/**
 * Title:		KFrogLogical.java
 * Description: This class is used to represent logical data type. 
 * 
 * @version		1.0 May 2009
 * @author		Rami Habas
 */

package kfrog.type;

public class KFrogLogical extends KFrogDataType {

	/** variable value */
	private boolean variable;

	/**
     * Constructor used to instantiate a variable of logical type
     * 
     * @param variable   The initial value
     */
	public KFrogLogical(boolean variable) {
		super();
		this.variable = variable;
		init = true;
	}

	/**
     * Default Constructor instantiates a variable with a value of false.
     */
	public KFrogLogical() {
		this(false);
		init = false;
	}

	/**
     * Useful method that returns the boolean value of the given KFrogDataType
     * variable
     * 
     * @param b   KFrogDataType variable
     */
	public static boolean logicalValue(KFrogDataType b) {

		b.initCheck();

		if (b instanceof KFrogLogical)
			return ((KFrogLogical)b).variable;

		throw new RuntimeException("Cannot cast to logical value");
	}

	/** logical type copy method */
	public KFrogDataType copy() {
		initCheck();
		return new KFrogLogical(variable);
	}

	/** instantiate a new logical type */
	public KFrogDataType newType() {
		return new KFrogLogical();
	}

	public String typeName() {
		return "logical";
	}

	/** get the logical value as String */
	public String getValue() {
		initCheck();
		return String.valueOf(variable);
	}

	/** get the logical value as a boolean */
	public boolean getVariable() {
		initCheck();
		return variable;
	}

	/**
     * Perform the assignment a=b where a is this instance. Note that this can
     * also serve as an initialization so init=true.
     * 
     * @param b   KFrogDataType variable
     */
	public KFrogDataType assign(KFrogDataType b) {
		variableCheck();
		b.initCheck();
		variable = logicalValue(b);
		init = true;
		return this;
	}

	public KFrogDataType isEqual(KFrogDataType b) {
		initCheck();
		return new KFrogLogical(variable == logicalValue(b));
	}

	public KFrogDataType or(KFrogDataType b) {
		initCheck();
		return new KFrogLogical(variable || logicalValue(b));
	}

	public KFrogDataType and(KFrogDataType b) {
		initCheck();
		return new KFrogLogical(variable && logicalValue(b));
	}

	public KFrogDataType notEqual(KFrogDataType b) {
		initCheck();
		return new KFrogLogical(variable != logicalValue(b));
	}

	public KFrogDataType not() {
		initCheck();
		return new KFrogLogical(!variable);
	}
}
